package com.alumni_circle.controller.gossip;

import com.alumni_circle.core.config.SysLog;
import com.alumni_circle.core.entity.BaseId;
import com.alumni_circle.core.entity.PageBase;
import com.alumni_circle.core.entity.ResponseListModel;
import com.alumni_circle.core.entity.ResponseModel;
import com.alumni_circle.core.utils.ModelUtils;
import com.alumni_circle.core.utils.SecurityUtils;
import com.alumni_circle.core.web.BaseController;
import com.alumni_circle.entity.gossip.Comments;
import com.alumni_circle.entity.gossip.VO.CommentsInsertVO;
import com.alumni_circle.entity.gossip.VO.CommentsViewVO;
import com.alumni_circle.entity.home.issue.GossipComments;
import com.alumni_circle.entity.home.issue.UserComments;
import com.alumni_circle.entity.me.User;
import com.alumni_circle.service.gossip.ICommentsService;
import com.alumni_circle.service.gossip.impl.GossipServiceImpl;
import com.alumni_circle.service.home.issue.IGossipCommentsService;
import com.alumni_circle.service.home.issue.IUserCommentsService;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 龙小虬
 * @since 2020-07-07 13:33
 */
@Api(tags = "评论管理")
@RestController
@RequestMapping("/comments")
public class CommentsController extends BaseController<Comments> {

    @Resource
    private ICommentsService commentsService;

    @Resource
    private IUserCommentsService userCommentsService;

    @Resource
    private IGossipCommentsService gossipCommentsService;

    @Resource
    private GossipServiceImpl gossipService;

    /**
     * 增加信息
     * @param commentsInsertVO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addComments", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "新增评论信息", notes = "增加评论信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="增",accessInfo="新增评论信息",url="/comments/addComments")
    public ResponseModel<String> addComments(@ApiParam(required = true, name = "commentsInsertVO")
                                   @RequestBody CommentsInsertVO commentsInsertVO) {
        Comments comments = ModelUtils.transform(commentsInsertVO,Comments.class);
        User user = SecurityUtils.getUser();
        comments.setCreateBy(user.getId());
        comments.setCreateName(user.getName());
        commentsService.save(comments);
        UserComments userComments = new UserComments();
        userComments.setUserId(user.getId());
        userComments.setCommentsId(comments.getId());
        userCommentsService.save(userComments);
        GossipComments gossipComments = new GossipComments();
        gossipComments.setCommentsId(comments.getId());
        gossipComments.setGossipId(commentsInsertVO.getGossipId());
        gossipCommentsService.save(gossipComments);
        gossipService.updateComments(commentsInsertVO.getGossipId());
        return new ResponseModel<>("success");
    }

    /**
     * 更新信息
     * @param baseId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateComments", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "更新评论信息", notes = "更新评论信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="改",accessInfo="更新评论信息",url="/comments/updateComments")
    public ResponseModel<String> updateComments(@ApiParam(required = true, name = "comments")
                                      @RequestBody BaseId baseId) {

        commentsService.updateCur(baseId.getId());
        return new ResponseModel<>("success");
    }

    @ResponseBody
    @RequestMapping(value = "/selectComments", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询评论信息", notes = "查询评论信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询评论信息",url="/comments/selectComments")
    public ResponseModel selectComments(@ApiParam(required = true, name = "baseId")
                                      @RequestBody BaseId baseId) {

//        Comments comments = commentsService.getById(baseId.getId());
//        return new ResponseModel<>(ModelUtils.transform(comments, CommentsInsertVO.class));
        return null;
    }

    @ResponseBody
    @RequestMapping(value = "/deleteComments", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "删除评论信息", notes = "删除评论信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="删",accessInfo="新增评论信息",url="/comments/deleteComments")
    public ResponseModel deleteComments(@ApiParam(required = true, name = "baseId")
                                      @RequestBody BaseId baseId) {

//        commentsService.removeById(baseId.getId());
//        QueryWrapper<UserComments> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("comments_id",baseId.getId());
//        userCommentsService.remove(queryWrapper);
//        return new ResponseModel();
        return null;
    }


    @ResponseBody
    @RequestMapping(value = "/getAllComments", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询所有评论信息", notes = "查询所有评论信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询所有评论信息",url="/comments/getAllComments")
    public ResponseListModel<Comments> getAllComments(@ApiParam(required = true, name = "ResponseListModel")
                                          @RequestBody BaseId baseId) {
//        return new ResponseListModel<>(commentsService.list());
        return null;
    }

    @ResponseBody
    @RequestMapping(value = "/selectListComments", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "查询评论信息", notes = "查询评论信息", httpMethod = "POST", response = ResponseModel.class)
    @SysLog(type="查",accessInfo="查询评论信息",url="/comments/selectListComments")
    public ResponseModel<List<CommentsViewVO>> selectListComments(@ApiParam(required = true, name = "CommentsInsertVO")
                                        @RequestBody PageBase pageBase) {
        PageHelper.startPage(pageBase.getPageNum(),pageBase.getPageSize());
        List<CommentsViewVO>list = commentsService.selectListComments(pageBase.getContent(),SecurityUtils.getId());
        //取消头像下载，使用微信提供的链接
//        for (int i = 0; i < list.size(); i++) {
//            list.get(i).setIcon(PictureBase64.imageStr(list.get(i).getIcon()));
//        }
        return new ResponseModel<>(list);
    }

}

